#!/bin/sh

# chkconfig: 2345 95 20
# description: Replication Manager is a MariDB HA replication architeture
# Startup script for ScrambleDB server and library
# Copyright (C) 2016 Stephane Varoqui @MariaDB
# All rights reserved.
# processname: replication-manager
#
# Use and distribution licensed under the LGPL license.  See
# the COPYING file in this directory for full text.

MRMDATADIR="/usr/share/replication-manager"
MRMBASEDIR="/usr/bin"
export MRMBASEDIR=$MRMBASEDIR
export MRMDATADIR=$MRMDATADIR
export MRMUSER=mrm
export MRMGROUP=mrm


groupadd -f $MRMGROUP
useradd  -g  $MRMGROUP $MRMUSER  > /dev/null 2>&1

chown -R $MRMUSER:$MRMGROUP $MRMDATADIR


CMD=$MRMBASEDIR/replication-manager
PIDFILE=$MRMDATADIR/mrm.pid


start()
{
  if [ -f  $PIDFILE ]
  then
  if ps -p `cat $PIDFILE` > /dev/null
  then
    echo "$PID is running"
    exit
  fi
  fi
su $MRMUSER <<EOF
nohup $CMD monitor --daemon  < /dev/null > $MRMDATADIR/out.log 2>&1 &
pidof -s replication-manager >  $PIDFILE
EOF
}

stop()
{
  kill `cat $PIDFILE`
}

case "$1" in
  start)
    start
  ;;
  stop)
    stop
  ;;
  restart)
    stop
    start
  ;;

  *)
    echo "Usage: $0 {start|stop|restart} "
  ;;

esac
